<?php
class InfoAction extends IntesysAction {
	public function _initialize(){
		parent::_initialize();
		$this->dao=D('Info');
		$_POST['site_id']=__SITEID__;	
	}
	//显示出来一个类似QQ空间的列表
	public function index(){
		
	    //分为两种方式返回页面内容
		if(IS_AJAX){
			
			if($this->_get('mine') && $this->_get('mine') != ''){
				$map['member_id'] = $this->memberInfo['id'];
			}else{
				$map['share'] = 1;
			}
			if($this->_get('member_id') && intval($this->_get('member_id')) > 0){
				$map['member_id'] = $this->_get('member_id');
			}
			//返回JSON列表
			$this->initView();
			$map['site_id'] = __SITEID__;
			//$map['member_id'] = $this->memberInfo['id'];	
			$map['pid'] = 0;
			$this->dao ->_list($this->view,$map, 'id desc', 5);
			$list = $this-> view -> get('list');
			foreach($list as $k => $v){
				//$str = str_replace('<!--sub#'.$v['id'].'-->', $this->tree($v['id']), $str);
				$list[$k]['likeList'] = M('Zan') -> where(array('info_id' => $v['id'])) -> select();
				
				
				$list[$k]['images'] = explode(',', $v['images']);
				
				
			}
			$this->assign('list', $list);
			$str = $this -> fetch();
			//dump($this -> get('list'));
			
			$list = $_GET['list'];
			$empty = 1;
			$statu=$list?1:0;
			if($this->get('list')){
				$empty = 0;
			}
			//$this->initView();
			die(json_encode(array('empty' => $empty,'tbody'=>$str,'dump'=>$_REQUEST,'statu'=>$statu, 'pages' => $this-> view -> get('pages'))));	 
			
		}else{
			//显示内容列表框架
			$this->display();
		}
	}
	//得到一个无限级信息列表
	public function tree($pid = 0){
		$list = $this->dao -> where(array('site_id' => __SITEID__, 'pid' => $pid)) -> select();
		if($list){
			$this->assign('list', $list);
			$curstr = $this->fetch('tree');
			foreach($this->list as $k => $v){
			    $curstr = str_replace('<!--sub#'.$v['id'].'-->', $this->tree($v['id']), $curstr);
		    }
			return $curstr;
		}
	}
	//单独获取评论列表
	public function cmntList(){
		$this->initView();
		$map['site_id'] = __SITEID__;
		$map['pid'] = $_GET['pid'];
		$this->dao ->_list($this->view,$map, 'id desc', 5);
		
		$str = $this->fetch('tree');
		$list = $this->view -> get('list');
		foreach($list as $k => $v){
			$str = str_replace('<!--sub#'.$v['id'].'-->', $this->tree($v['id']), $str);
		}
		
		$list = $_GET['list'];
		$empty = 1;
		$statu=$list?1:0;
		if($this->get('list')){
			$empty = 0;
		}
		$this->initView();
		die(json_encode(array('empty' => $empty,'tbody'=>$str,'dump'=>$_REQUEST,'statu'=>$statu, 'pages' => $this-> view -> get('pages'))));	
		
	}
	
	
	//发表评论
	public function insert(){
		$data['member_id'] = $this->memberInfo['id'];
		$data['remark'] = inemoji($this->_post('remark'));
		if(empty($data['remark'])){
			$this->error('错误！内容不能为空！');
			die();
		}
		$data['pid'] = (int)$_POST['pid'];
		$data['share'] = 1;
		$data['images'] = implode(',', $_POST['img_url']);
		$data['datetime'] = time(); 
		$data['headimg'] = $this->memberInfo['img_url'];
		$data['membername'] = $this->memberInfo['name'] == '' ? $this->memberInfo['nickname'] : $this->memberInfo['name'];
		$data['site_id'] = __SITEID__;
		$info = $this -> dao -> where(array('id' => $data['pid'])) -> find();
		$tomember = M('Member') -> where(array('id' => $info['member_id'], 'site_id' => __SITEID__)) -> find();
		if($tomember){
			$data['tomembername'] = $tomember['nickname'];
		    $data['tomemberid'] = $tomember['id'];
		}
		
		$ret = $this->dao -> add($data);
		if($ret){
			$this -> dao -> where(array('id' => $data['pid'])) -> setInc('evcount');
			$this->success('发表成功');
		}else{
			$this->error($this->dao -> getError());
		}
	}
	
	//删除日志
	public function delete(){
		$info_id = intval($this->_get('id'));
		if($info_id == 0){
			$this->error('参数错误');exit;
		}else{
			$info = $this->dao -> where(array('id' => $info_id)) -> find();
			if($info['member_id'] == $this->memberInfo['id']){
				$ret = $this->delev($info_id);
				if($ret){
					$this->success('删除成功');
				}else{
					$this->error($this->dao -> getError());
				}
			}else{
				$this->error('您不能删除他人数据！');exit;
			}
		}
	}
	//删除评论
	private function delev($id = 0){
		$info_id = $id;
		$delret = $this->dao -> where(array('id' => $info_id)) -> delete();
		if($delret){
			//$this -> dao -> where(array('id' => $info_id)) -> setDec('evcount');
			$list = $this->dao -> where(array('pid' => $info_id)) -> select();
			foreach($list as $k => $v){
				//递归删除子评论
				$this->delev($v['id']);
			}
		}else{
			return false;
		}
		return true;
	}
	//点赞
	public function like(){
		$pid = $this->_get('id')?$this->_get('id'):0;
		if($pid == 0){
			$this->error('参数错误！');
		}
		//是否点过
		$zanDB = M('Zan');
		$ret = array();
		$info = $zanDB -> where(array('info_id' => $pid, 'member_id' => $this->memberInfo['id'])) -> find();
		//$tomember = M('Member') -> where(array('id' => $info['member_id'], 'site_id' => __SITEID__)) -> find();
		if($info){
			
			
			$re = $zanDB -> where(array('info_id' => $pid, 'member_id' => $this->memberInfo['id'])) -> delete();
			
			if($re){
				$ret['data'] = $zanDB -> where(array('info_id' => $pid)) -> count();
				$this->dao -> where(array('id' => $pid)) ->data(array('likecount' => $ret['data'])) -> save();
				$ret['status'] = 1;
				$ret['info'] = '取消点赞成功！';
				echo json_encode($ret);
			}else{
				$this->error($zanDB -> getError());
			}
		}else{
			$data['info_id'] = $pid;
			$data['member_id'] = $this->memberInfo['id'];
			$data['datetime'] = time();
			$data['membername'] = $this->memberInfo['nickname'];
			$data['headimg'] = $this->memberInfo['img_url'];
			$re = $zanDB -> add($data);
			if($re){
				$ret['data'] = $zanDB -> where(array('info_id' => $pid)) -> count();
				$this->dao -> where(array('id' => $pid)) ->data(array('likecount' => $ret['data'])) -> save();
				$ret['status'] = 1;
				$ret['info'] = '点赞成功！';
				echo json_encode($ret);
			}else{
				$this->error($zanDB -> getError());
			}
		}
	}
	
	
	
	function likeList(){
		$this->list = M('Zan') -> where(array('info_id' => $_GET['info_id'])) -> select();
		$this->pages = '';
		$this->display();
	}
	
	
	
	
	//收藏
	public function collect(){
		
	}
	
	//发送信息的方法
	//
	//@content 		信息的内容
	//@member_id 	用户ID
	//
	public function sendMsg(){
		if(IS_AJAX){
			$this->dao->sendMsg([['content'=>$this->_parm('content'),'mode'=>C('MSG_TYPE'),'tomemberid'=>$this->_parm('member_id'),'type'=>'remind']]);
			echo json_encode(['dump'=>$_REQUEST,'status'=>1]);
		}
	}
	
	//赞赏奖票
	private function giveTiket($member_id, $info_id, $num = 1){
		$db = M('Ticket');
		$data['time'] = time();
		$data['member_id'] = $member_id;
		$data['number'] = $num;
		$data['info_id'] = $info_id;
		$data['site_id'] = __SITEID__;
		$data['remark'] = '发布动态奖票#'.$info_id.'#';
		$data['giver_id'] = $this->memberInfo['id'];
		$data['rewarder_id'] = $this->memberInfo['id'];
		$db -> add($data);
		$tiketcount['tiketcount'] = $db -> where(array('site_id' => __SITEID__, 'info_id' => $info_id)) -> count();
		$this -> dao -> where(array('site_id' => __SITEID__, 'id' => $info_id)) -> save($tiketcount);
	}
	
	
	//giveintegral//赞赏积分
	public function giveintegral(){
		$approvername=$this->memberInfo['name'];

		if(IS_POST){
			$flag = true;
			foreach($_POST['integral'] as $k => $v){
				//是否在权限分数以内
				if($v <= 0 || $v > $this -> integralconfig[$k]){
					$flag = false;
				}
			}
			if($flag){
				//正常分配积分($num,$ruleid,$memberid,$remark,$rewarderid,$time,$siteid)
				foreach($_POST['integral'] as $k => $v){
				//是否在权限分数以内
				    import("@.ORG.Integrals.".$k);
					$inte = new $k($this->_post('member_id'));
				    $inte -> giveIntegralMore($v, 0, $this->_post('member_id'), '分享动态加分', $this->memberInfo['id'], time(), __SITEID__,$approvername);
					
					//查找积分类型ID和名称
					$integraltype = M('Integral_type') -> where(array('site_id' => __SITEID__,'typename' => $k)) -> find();
					$insertinfointegral['member_id'] = $this->_post('member_id');
					$insertinfointegral['integraltypename'] = $integraltype['remark'];
					$insertinfointegral['integraltypeid'] = $integraltype['id'];
					$insertinfointegral['giver_id'] = $this->memberInfo['id'];
					$insertinfointegral['integralnum'] = $v;
					$insertinfointegral['dateline'] = time();
					$insertinfointegral['site_id'] = __SITEID__;
					$insertinfointegral['info_id'] = $this->_post('info_id');
					$integraldb = M('Infointegral');
					$integraldb -> add($insertinfointegral);
					$infointegralcount['integralcount'] = $integraldb -> where(array('site_id' => __SITEID__, 'info_id' => $this->_post('info_id'))) -> count();
					$this->dao -> where(array('id' => $this -> _post('info_id'), 'site_id' => __SITEID__)) -> data($infointegralcount) -> save();
					 
				}
				if($this->_post('tiketnum') > 0){
					$this->giveTiket($this->_post('member_id'), $this->_post('info_id'));
				}
				$this->success();
			}else{
				$this->error('分配分数超出权限');
			}
		}else{
			$this->member_id = $this->_get('member_id');
			$this->info_id = $this->_get('info_id');
			$this->display();
		}
	}
}

